/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VisualizarImagenesDescargadas.java
 *
 * Created on 26/02/2012, 10:46:50 AM
 */

package proy1sistdistribuidos.interfaz;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import proy1sistdistribuidos.modelo.AdministradorJXTA;
import proy1sistdistribuidos.modelo.ControladorProySistDistribuidos;

/**
 * Forma para la visualización de imágenes descargadas
 * @author Carlos Diaz Maya
 */
public class VisualizarImagenesDescargadas extends javax.swing.JFrame implements Observer  {

    private PTableModel modeloJTableImgDesc;
    ControladorProySistDistribuidos cont;
    ArrayList columnas = new ArrayList();

    /** Creates new form VisualizarImagenesDescargadas */
    public VisualizarImagenesDescargadas() {
        prepararModeloTablaImgDesc();

        initComponents();
        inicializarTablaImgDesc();
    }
    
     public VisualizarImagenesDescargadas(ControladorProySistDistribuidos cont) {
        this.cont = cont;
        prepararModeloTablaImgDesc();
        initComponents();
        inicializarTablaImgDesc();
    }

    public void prepararModeloTablaImgDesc() {
        columnas.add("Etiqueta");
        columnas.add("Fecha de creación");
        columnas.add("Fecha de descarga");
        columnas.add("Nombres de personas");
        columnas.add("Ruta del archivo de imagen");
        modeloJTableImgDesc = new PTableModel(columnas);
    }

    public void inicializarTablaImgDesc() {
        modeloJTableImgDesc.clear();
        File[] archivosDescargados = cont.obtenerArchivosImagenDescargados();
        int i=0;
        for(File archivo:archivosDescargados){
            List<String[]> infPersonas = null;
            try {
                infPersonas = cont.obtenerInformacionArchivoInfPersonas(archivo);
            } catch (Exception ex) {
                Logger.getLogger(VisualizarImagenesDescargadas.class.getName()).log(Level.SEVERE, null, ex);
            }
            List<String> infDescarga = null;
            try {
                infDescarga = cont.obtenerInformacionArchivoInfDescarga(archivo);
            } catch (Exception ex) {
                Logger.getLogger(VisualizarImagenesDescargadas.class.getName()).log(Level.SEVERE, null, ex);
            }

            ArrayList<Object> fila = new ArrayList<Object>();

            if(infDescarga!=null){
                fila.add(infDescarga.get(0));
                fila.add(infDescarga.get(1));
                fila.add(infDescarga.get(2));
                if(infPersonas!=null){
                    String cadenaPersonas = "";
                    boolean primero = true;
                    for(String[] tupla:infPersonas){
                        if(!primero) cadenaPersonas += ", ";
                        else {
                            primero = false;
                        }
                        cadenaPersonas += tupla[0] + " " + tupla[1];
                    }
                    fila.add(cadenaPersonas);
                } else {
                     fila.add("");
                }
                fila.add(archivo.getAbsolutePath());
                modeloJTableImgDesc.addRow(i, fila);
                i++;
            }

            
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableImgDesc = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(proy1sistdistribuidos.interfaz.Principal.class).getContext().getResourceMap(VisualizarImagenesDescargadas.class);
        jLabel1.setText(resourceMap.getString("jLabelImgDesc1.text")); // NOI18N
        jLabel1.setName("jLabelImgDesc1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableImgDesc.setModel(modeloJTableImgDesc);
        jTableImgDesc.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTableImgDesc.setName("jTableImgDesc"); // NOI18N
        jScrollPane1.setViewportView(jTableImgDesc);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 421, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(60, 60, 60))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jButton1.getAccessibleContext().setAccessibleName(resourceMap.getString("jButton1.AccessibleContext.accessibleName")); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.cont.removeObserver(this);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int numFila = this.jTableImgDesc.getSelectedRow();
        ArrayList fila = this.modeloJTableImgDesc.getRowAt(numFila);
        try{
            VisualizarImagenDescargada vis = new VisualizarImagenDescargada(fila, cont, this);
            vis.setVisible(true);
            this.setVisible(false);
        } catch (Exception f) {
            f.printStackTrace();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VisualizarImagenesDescargadas().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableImgDesc;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
       
    }

}
